# -*- coding: utf-8 -*-
"""Configuration file reader"""
__author__ = "Lei Chen <hide1713@gmail.com>"
import logging
import ConfigParser
import os.path

class GenomeConfig(object):
    """This class loads configuration file and store the configuration
    """
    
    def __init__(self):
        """
        """
        self.log = logging.getLogger(self.__class__.__name__)
        self.config = ConfigParser.RawConfigParser()
        self.cog_file_path = ""
        self.genome_data_folder = ""
        self.hitp_data_folder = ""
        self.logging_level = logging.INFO
        self.config_file = ""
        self.default_password = ""
        self.url_base = ""
        
    def print_config(self):
        print "---Current Config---"+self.config_file
        for s in self.config.sections():
            for opt in self.config.options(s):
                print opt,"=",self.config.get(s,opt)
        

    def read_file(self, file_name):
        """Read the configuration file
        
        Arguments:
        - `file_name`:
        """
        if os.path.exists(file_name):
            self.config_file = file_name
            self.config.read(file_name)
            try:
                self.url_base = self.config.get('ServerOption','url_base')
                self.default_password = self.config.get('ServerOption', 'default_password')
                self.cog_file_path = self.config.get('Directories', 'cog_file_path')
                if not os.path.isfile(self.cog_file_path):
                    raise Exception("Cog family file not find. Check your configuration file")
                self.genome_data_folder = self.config.get('Directories', 'genome_data_folder')
                if not os.path.isdir(self.genome_data_folder):
                    raise Exception("Genome data folder not find.Check your configuration file")
                self.hitp_data_folder = self.config.get('Directories', 'hitp_data_folder')
                if not os.path.isdir(self.hitp_data_folder):
                    raise Exception("Hitp data folder not find.Check your configuration file")
                if "LogLevel" in self.config.sections():
                    LEVELS =  {'debug': logging.DEBUG,
                               'info': logging.INFO,
                               'warning': logging.WARNING,
                               'error': logging.ERROR,
                               'critical': logging.CRITICAL}
                    try:
                        self.logging_level = LEVELS[self.config.get('LogLevel','log_level')]
                    except KeyError:
                        self.log.error("Wrong logging level. Use default")
            except Exception, e:
                self.log.error(e)
        else:
            self.log.error("Configuration file not exists:" + file_name)

        

        


